/** 
 * Project Name:qfhy 
 * File Name:BaseDaoImpl.java 
 * Package Name:com.wf.qfhy.dao.impl 
 * Date:2017年4月26日上午11:10:23 
 * Copyright (c) 2017, wxwj All Rights Reserved. 
 */

package com.wf.qfhy.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.wf.qfhy.dao.IBaseDao;

/**
 * @ClassName: BaseDaoImpl
 * @Description:
 * @author 王飞
 * @date 2017年4月26日 上午11:10:23
 * 
 */
public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {

	@Resource
	public void setSessionFacotry(SessionFactory sessionFacotry) {
		super.setSessionFactory(sessionFacotry);
	}

	@SuppressWarnings("unchecked")
	public Class<T> getEntityClass() {
		return (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	@Override
	public Serializable save(T o) {
		return getHibernateTemplate().save(o);
	}

	@Override
	public T getById(Class<T> c, Serializable id) {
		return getHibernateTemplate().get(c, id);
	}

	@Override
	public T get(String hql) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		List<T> l = q.list();
		if (l != null && l.size() > 0) {
			return l.get(0);
		}
		return null;
	}

	@Override
	public T getByParams(String hql, Map<String, Object> params) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		List<T> l = q.list();
		if (l != null && l.size() > 0) {
			return l.get(0);
		}
		return null;
	}

	@Override
	public void delete(T o) {
		getHibernateTemplate().delete(o);
	}

	@Override
	public void update(T o) {
		getHibernateTemplate().update(o);
	}

	@Override
	public void saveOrUpdate(T o) {
		getHibernateTemplate().saveOrUpdate(o);
	}

	@Override
	public List<T> find(String hql) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		return q.list();
	}

	@Override
	public List<T> findByParams(String hql, Map<String, Object> params) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return q.list();
	}

	@Override
	public List<T> findPagedListByParams(String hql, Map<String, Object> params, int page, int rows) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public List<T> findPagedList(String hql, int page, int rows) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public Long count(String hql) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		return (Long) q.uniqueResult();
	}

	@Override
	public Long countByParams(String hql, Map<String, Object> params) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return (Long) q.uniqueResult();
	}

	@Override
	public int executeHql(String hql) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		return q.executeUpdate();
	}

	@Override
	public int executeHqlByParams(String hql, Map<String, Object> params) {
		Query q = getSessionFactory().getCurrentSession().createQuery(hql);
		if (params != null && !params.isEmpty()) {
			for (String key : params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return q.executeUpdate();
	}
}
